﻿using Ant.Interface.Data.Migration.Generator;
using NHibernate.Tool.hbm2ddl;

namespace Ant.BasicPlugIn.Data.Migration.Generator
{
    public class SchemaCommandGenerator : ISchemaCommandGenerator
    {
        private readonly ISessionFactoryHolder _sessionFactoryHolder;

        public SchemaCommandGenerator(ISessionFactoryHolder sessionFactoryHolder)
        {
            _sessionFactoryHolder = sessionFactoryHolder;
        }

        /// <summary>
        /// 自动更新数据库中的表。
        /// </summary>
        public void UpdateDatabase()
        {
            var configuration = _sessionFactoryHolder.GetConfiguration();
            new SchemaUpdate(configuration).Execute(false, true);
        }

        /// <summary>
        /// 在数据库中创建表。
        /// </summary>
        public void CreateDatabase()
        {
            var configuration = _sessionFactoryHolder.GetConfiguration();
            new SchemaExport(configuration).Execute(false, true, false);
        }
    }
}